package worker;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import bookshopSM.BookshopGUI;

public class ResetWorkerCode extends JPanel{
    private BookshopGUI userInterface1;
    private JButton  firstButton1,secondButton1, firstButton2,secondButton2;
    int vnoDelete;
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";

    public ResetWorkerCode() throws SQLException{

        JLabel ltitle = new JLabel("重置员工密码        ");
        ltitle.setFont(new   java.awt.Font("楷体", 1, 20));
        setLayout(new FlowLayout());
        String names1[]={"请输入员工号："};
        userInterface1 = new BookshopGUI(names1 );
        Box box = Box.createVerticalBox();
        box.add(Box.createVerticalStrut(15));
        box.add(ltitle );
        box.add(Box.createVerticalStrut(15));
        box.add(userInterface1 );
        //************修改边界布局各个框的大小**************
        //box1.setPreferredSize(new Dimension(350, 100));
        //box2.setPreferredSize(new Dimension(300, 150));

	      /*Icon bug = new ImageIcon(getClass().getResource("bgp.jpg" ));
		  JLabel label = new JLabel( bug, SwingConstants.CENTER );
		  this.add(label,BorderLayout.CENTER);*/
        this.add(box);
        setVisible( true );

        firstButton1 = userInterface1.getDoTask1Button();
        firstButton1.setText( "确认" );
        firstButton1.addActionListener(
                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            updateRecord();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
        );

        secondButton1 = userInterface1.getDoTask2Button();
        secondButton1.setText( "清除" );
        secondButton1.addActionListener(

                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface1.clearFields();
                    }
                }
        );

        setSize( 500, 360 );
        setVisible( true );
    }


    private void updateRecord() throws SQLException {
        // TODO Auto-generated method stub
        conn = DriverManager.getConnection(url,username,password);
        Statement stmt=conn.createStatement();
        String fieldValues[] = userInterface1.getFieldValues();
        try {
            //"姓名","性别","联系方式","等级","总消费额","折扣额度","住址","备注"
            stmt.executeUpdate("update Worker set" +
                    " Wcode='123456'"+
                    " where Wno="+Integer.valueOf(fieldValues[0]));
            JOptionPane.showMessageDialog(null, "修改成功");
        }

        catch (Exception ee){
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }
}
